package cn.shiwei.pet.controller;

import cn.shiwei.basic.exception.MyException;
import cn.shiwei.basic.util.DistanceUtil;
import cn.shiwei.basic.util.LoginContextUtil;
import cn.shiwei.basic.util.PageList;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.pet.domain.Pet;
import cn.shiwei.pet.domain.SearchMasterMsg;
import cn.shiwei.pet.query.SearchMasterMsgQuery;
import cn.shiwei.pet.service.ISearchMasterMsgService;
import cn.shiwei.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.loadAll();
    }

    @PostMapping("/publish")
    public RequestStatus publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest httpServletRequest){
        try {
            Logininfo loininfo = LoginContextUtil.getLoininfo(httpServletRequest);
            seachMasterMsgService.publish(searchMasterMsg, loininfo);
            return RequestStatus.me();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙!!!");
        }
    }

    @PostMapping("/pending")
    public PageList<SearchMasterMsg> getList(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        Long logininfo_id = LoginContextUtil.getLoininfo(request).getId();
        return seachMasterMsgService.queryDate(query, logininfo_id);
    }

    @PutMapping("/handle")
    public RequestStatus handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Long logininfo_id = LoginContextUtil.getLoininfo(request).getId();
            seachMasterMsgService.handle(pet, logininfo_id);
            return RequestStatus.me();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统异常");
        }
    }

    @PostMapping("/processed")
    public PageList<SearchMasterMsg> getProcessedList(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        return seachMasterMsgService.queryDate(query);
    }
}
